using PureMVC.Interfaces;
using PureMVC.Patterns.Mediator;

namespace CounterApp.PureMVC
{
    public class CounterAppViewMediator : Mediator
    {
        public const string NAME = "COUNTER_VIEW";

        public CounterAppView View
        {
            get { return ViewComponent as CounterAppView; }
        }


        public CounterAppViewMediator(string mediatorName, object view = null) : base(mediatorName, view)
        {
        }

        // PureMVC 中的事件注册方式
        public override string[] ListNotificationInterests()
        {
            return new[] {"UPDATE_VIEW"};
        }

        public override void OnRegister()
        {
            View.BtnAdd.onClick.AddListener(() =>
            {
                // 发送增加 Command 事件
                Facade.SendNotification(IncreaseCommand.KEY);
            });

            View.BtnSub.onClick.AddListener(() =>
            {
                Facade.SendNotification(DecreaseCommand.KEY);
            });
        }


        public override void HandleNotification(INotification notification)
        {
            // 处理事件
            if (notification.Name == "UPDATE_VIEW")
            {
                View.NumberText.text = notification.Body.ToString();
            }
        }

        public override void OnRemove()
        {
            ViewComponent = null;
        }
    }
}